/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.vrangogym.persistence.entities;

import edu.chl.vrangogym.persistence.entities.keys.BookingPK;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

/**
 * Booking entity
 * @author habbbe
 * 
 * @revidedBy Ulken (2011-10-16)
 */
@Entity
@IdClass(BookingPK.class)
public class Booking implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * Composite key: A booking is uniquely defined by its user and corresponding 
     * workout session
     */
    @Id
    @ManyToOne
    private User booker;
    @Id
    @OneToOne(cascade = CascadeType.PERSIST)
    private WorkoutSession workoutSession;
    private int participants;
    @OneToOne
    private Collection<Machine> machines;

    public Booking() {
    }

    /**
     * Instantiate a new booking
     * @param booker the booker
     * @param workoutSession the workout session
     * @param participants number of participants
     */
    public Booking(User booker, WorkoutSession workoutSession, int participants) {
        this(booker, workoutSession, participants, new ArrayList<Machine>());
    }

    /**
     * Instantiate a new booking
     * @param booker the booker
     * @param workoutSession the workout session
     * @param participants number of participants
     * @param machines the machines booked
     */ 
    public Booking(User booker, WorkoutSession workoutSession, int participants, Collection<Machine> machines) {
        this.booker = booker;
        this.workoutSession = workoutSession;
        this.participants = participants;
        this.machines = machines;
    }

    /**
     * Used to simplify get of PK
     */
    public BookingPK getId() {
        return new BookingPK(booker.getUserName(), workoutSession.getStartTime());
    }

    /**
     * Immutable: Should not be able to alter booker once set
     */
    public User getBooker() {
        return booker;
    }

    public Collection<Machine> getMachines() {
        return machines;
    }

    public void setMachines(Collection<Machine> machines) {
        this.machines = machines;
    }

    public int getParticipants() {
        return participants;
    }

    public void setParticipants(int participants) {
        this.participants = participants;
    }

    /**
     * Immutable: Should not be able to alter workout session once set
     */
    public WorkoutSession getWorkoutSession() {
        return workoutSession;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Booking other = (Booking) obj;
        if (this.booker != other.booker && (this.booker == null || !this.booker.equals(other.booker))) {
            return false;
        }
        if (this.workoutSession != other.workoutSession && (this.workoutSession == null || !this.workoutSession.equals(other.workoutSession))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.booker != null ? this.booker.hashCode() : 0);
        hash = 97 * hash + (this.workoutSession != null ? this.workoutSession.hashCode() : 0);
        return hash;
    }
    
    
}
